stackView  =  [];
mapsView = new Object();

function ViewController(controller, params){
	this.controller = controller;
	//this.params = para
};


exports.navigateToView =  function(viewName, args, preventGoBack){
	if(mapsView[viewName]) {
		return;
	}
	mapsView[viewName] = viewName;
	var nextViewController = Alloy.createController(viewName, args);
	var controller = new ViewController(nextViewController, args);
	stackView.push(controller);
	nextViewController.getView().addEventListener('android:back', ( preventGoBack ? noGoBack : goBack ) );
	nextViewController.getView().open(); 
};
exports.goBack =  function(){
	goBack();
};	

function noGoBack() {
	Ti.API.log("log: No go back");
	Ti.API.debug("debug: No go back");
};

function goBack() {
	if(stackView.length > 0){
		var actual = stackView.pop();
		delete mapsView[actual.controller.__controllerPath];
		actual.controller.getView().removeEventListener('android:back', goBack );
		actual.controller.getView().close();
	} 
};

exports.exit = function() {
	while(stackView.length > 0) {
		goBack();
	}
};
exports.goHome =  function(){
	 // clear stack view
     while(true) {
   		if( stackView.length == 0 ) {
     		break;
	    }
     	var lastView = stackView[stackView.length-1];	
     	if( lastView.controller.__controllerPath === "Home" ) { //isHome && lastView.controller.isHome()
     		break;
     	}
		var actual = stackView.pop();
		delete mapsView[actual.controller.__controllerPath];
		actual.controller.getView().close();
     };
     
};	

exports.goLogin =  function(){
	 // clear stack view
     while(true) {
   		if( stackView.length == 0 ) {
     		break;
	    }
     	var lastView = stackView[stackView.length-1];	
     	if( lastView.controller.__controllerPath.toLowerCase().indexOf("login") > -1) { //isHome && lastView.controller.isHome()
     		break;
     	}
		var actual = stackView.pop();
		delete mapsView[actual.controller.__controllerPath];
		actual.controller.getView().close();
     };
     
};	
